<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>

<jsp:include page="/includes/top.jsp"></jsp:include>
<div class="page-header">
	<h1></h1>
</div>
<!-- /.page-header -->
<div class="row">
	<div class="col-xs-12">
		<form class="form-horizontal" id="uiform">
			<div style="display: none">
				<input type="hidden" name="users.userId" id="hidUserId"
					value="${users.userId}" />
			</div>
			<div class="form-group">
				<label class="col-sm-3 control-label no-padding-right">登录名</label>
				<div class="col-sm-4">
					<div class="clearfix input-icon input-icon-right">
						<input type="text" name="users.userUsername" class="form-control"
							placeholder="登录名" id="txtUserName" value="${users.userUsername}" />
					</div>
				</div>
			</div>
			<div class="form-group edithid">
				<label class="col-sm-3 control-label no-padding-right">密码</label>
				<div class="col-sm-4">
					<div class="clearfix input-icon input-icon-right">
						<input type="password" name="users.userPassword"
							class="form-control" placeholder="密码" />
					</div>
				</div>
			</div>
			<div class="form-group edithid">
				<label class="col-sm-3 control-label no-padding-right">确认密码</label>
				<div class="col-sm-4">
					<div class="clearfix input-icon input-icon-right">
						<input type="password" name="users.confirmPassword"
							class="form-control" placeholder="确认密码" />
					</div>
				</div>
			</div>
			<div class="form-group">
				<label class="col-sm-3 control-label no-padding-right">姓名</label>
				<div class="col-sm-4">
					<div class="clearfix input-icon input-icon-right">
						<input type="text" name="users.userinfoTruename"
							class="form-control" placeholder="姓名"
							value="${users.userinfoTruename}" />
					</div>
				</div>
			</div>
			<div class="form-group">
				<label class="col-sm-3 control-label no-padding-right">公司</label>
				<div class="col-sm-4">
					<div class="clearfix input-icon input-icon-right">
						<s:select cssClass="form-control" id="company"
							name="users.userinfoCompany" list="#clist "
							listValue="companyName" listKey="companyId"
							value="users.userinfoCompany"></s:select>
					</div>
				</div>
			</div>
			<div class="form-group">
				<label class="col-sm-3 control-label no-padding-right">部门</label>
				<div class="col-sm-4">
					<div class="clearfix input-icon input-icon-right">
						<s:select cssClass="form-control" id="depart"
							name="users.userinfoDepartment" list="#dlist "
							listValue="departmentName" listKey="departmentId"
							value="users.userinfoDepartment"></s:select>
					</div>
				</div>
			</div>
			<div class="form-group">
				<label class="col-sm-3 control-label no-padding-right">职务</label>
				<div class="col-sm-4">
					<div class="clearfix input-icon input-icon-right">
						<input type="text" name="users.userinfoPosition"
							class="form-control" placeholder="职务"
							value="${users.userinfoPosition}" />
					</div>
				</div>
			</div>
			<div class="form-group">
				<label class="col-sm-3 control-label no-padding-right">电话</label>
				<div class="col-sm-4">
					<div class="clearfix input-icon input-icon-right">
						<input type="text" name="users.userinfoPhone" class="form-control"
							placeholder="电话" value="${users.userinfoPhone}" />
					</div>
				</div>
			</div>
			<div class="form-group">
				<label class="col-sm-3 control-label no-padding-right">Email</label>
				<div class="col-sm-4">
					<div class="clearfix input-icon input-icon-right">
						<input type="text" name="users.userinfoEmail" class="form-control"
							placeholder="Email" value="${users.userinfoEmail}" />
					</div>
				</div>
			</div>
			<div class="form-group">
				<label class="col-sm-3 control-label no-padding-right">通讯地址</label>
				<div class="col-sm-4">
					<div class="clearfix input-icon input-icon-right">
						<input type="text" name="users.userinfoAddress"
							class="form-control" placeholder="通讯地址"
							value="${users.userinfoAddress}" />
					</div>
				</div>
			</div>
			<div class="form-group">
				<label class="col-sm-3 control-label no-padding-right">邮政编码</label>
				<div class="col-sm-4">
					<div class="clearfix input-icon input-icon-right">
						<input type="text" name="users.userinfoZip" class="form-control"
							placeholder="邮政编码" value="${users.userinfoZip}" />
					</div>
				</div>
			</div>
			<div class="form-group">
				<label class="col-sm-3 control-label no-padding-right">生日</label>
				<div class="col-sm-4">
					<div class="clearfix input-icon input-icon-right">
						<div class="input-group">
							<input type="text" name="users.userBirthday"
								value="${users.userBirthday}" class="form-control date-picker"
								placeholder="生日" /> <span class="input-group-addon"> <i
								class="icon-calendar bigger-110"></i>
							</span>
						</div>
					</div>
				</div>
			</div>
			<div class="clearfix form-actions">
				<div class="col-md-offset-3 col-md-9">
					<button class="btn btn-info" type="button" id="save">
						<i class="icon-ok"></i> 保存
					</button>

					&nbsp; &nbsp; &nbsp;
					<button class="btn" type="reset" id="back">
						<i class="icon-undo"></i> 取消
					</button>
				</div>
			</div>
		</form>
	</div>
	<!-- /.col -->
</div>
<jsp:include page="/includes/bottom.jsp"></jsp:include>

<script src="assets/js/bootstrapValidator.min.js"></script>

<script type="text/javascript">
	var uid = getQueryString("users.userId");
	$(document).ready(function() {
		$('.date-picker').datetimepicker({
			lang : 'ch',
			format : 'Y-m-d',
			timepicker : false
		}).next().on("click", function() {
			$(this).prev().focus();
		});
		if (uid != null && uid != "") {
			$(".edithid").hide();
			savafrom("edituser");
		} else {
			savafrom("adduser");
		}
		$("#company").change(function() {
			setselectval("depart","departall","departmentId","departmentName",{cid:$(this).val()});				
		});
		$("#back").click(function() {
			window.history.back();
		});
		validator();
	});

	function savafrom(url) {
		$("#save").click(function() {
			$("#uiform").bootstrapValidator("validate");
			if ($("#uiform").data("bootstrapValidator").isValid()) {
				var dataMap = $("#uiform").serializeArray();
				$.ajax({
					type : "post",
					url : url,
					data : dataMap,
					dataType : "json",
					success : function(data) {
						window.open("admin/userlist.jsp?m=" + mid, "_self");
					}
				});
			}
		});
	}

	function validator() {
		$("#uiform").bootstrapValidator({
			feedbackIcons : {
				valid : 'icon-ok-sign',
				invalid : 'icon-remove-sign',
				validating : 'icon-refresh'
			},
			fields : {
				'users.userUsername' : {
					validators : {
						notEmpty : {
							message : '用户名，不能为空！'
						},
						stringLength : {
							min : 5,
							max : 30,
							message : '用户名长度必须是5-30字符！'
						},
						regexp : {
							regexp : /^[a-zA-Z0-9_\.]+$/,
							message : '用户名只能输入数字或字母！'
						},
						remote : {
							url : 'validUserName',
							message : '用户名已使用请重新录入！',
							data : {
								"users.userId" : $("#hidUserId").val(),
								"users.userUsername" : $("#txtUserName").val()
							}
						}
					}
				},
				'users.userPassword' : {
					validators : {
						notEmpty : {
							message : '密码，不能为空！'
						}
					}
				},
				'users.confirmPassword' : {
					validators : {
						notEmpty : {
							message : '确认密码，不能为空！'
						},
						identical : {
							field : 'users.userPassword',
							message : '两次输入密码不一致！'
						}
					}
				},
				'users.userinfoTruename' : {
					validators : {
						notEmpty : {
							message : '姓名，不能为空！'
						}
					}
				},
				'users.userinfoCompany' : {
					validators : {
						notEmpty : {
							message : '公司，不能为空！'
						}
					}
				},
				'users.userinfoDepartment' : {
					validators : {
						notEmpty : {
							message : '部门，不能为空！'
						}
					}
				},
				'users.userinfoPhone': {
				    validators: {
				      notEmpty: {
				        message: '电话号码，不能为空！'
				      },
				      regexp : {
						regexp : /^[1][358]\d{9}$/,
						message : '电话号码格式错误,只能输入数字'
						}
				    }
				},
				'users.userinfoZip':{
				    regexp : {
						regexp : /^[1-9]\d{5}(?!\d)$/,
						message :'邮政编码格式错误,只能输入数字'
					}
				}
			}
		});
	}
</script>
